package com.homework.ch10;

/**
 * @author yuxi
 * @version 1.0
 * @date 2025/10/14
 * @Description:
 */
enum Xian{
    MEAT,VEGG
}
class Shop{
    private int meatCount=20;
    private int veggCount=40;
    public void makeBaozi(Xian type){
        synchronized (this) {
            switch (type) {
                case MEAT:
                    meatCount++;
                    System.out.println("制作了一个肉馅包子：" + meatCount);
                    break;
                case VEGG:
                    veggCount++;
                    System.out.println("制作了一个菜陷包子：" + veggCount);
            }
            this.notifyAll();
        }
    }
    public synchronized void sellBaozi(Xian type){
        switch (type){
            case MEAT:
                while(meatCount<=0) {
                    try {
                        System.out.println(Thread.currentThread().getName()+
                                "等待，当前肉包子没有了"+meatCount);
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                meatCount--;
                System.out.println(Thread.currentThread().getName()+
                        "买到肉包子了，还剩余"+meatCount);
                break;
            case VEGG:
                while(veggCount<=0) {
                    try {
                        System.out.println(Thread.currentThread().getName()+
                                "等待，当前蔬菜包子没有了"+veggCount);
                        this.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                veggCount--;
                System.out.println(Thread.currentThread().getName()+
                        "买到蔬菜包子了，还剩余"+veggCount);
                break;
        }
    }
}

public class Test11 {
    public static void main(String[] args) {
        Shop shop=new Shop();
        Thread chef=new Thread(){
            public void run(){
                while(true){
                    if(Math.random()>0.4){
                        shop.makeBaozi(Xian.MEAT);
                    }else{
                        shop.makeBaozi(Xian.VEGG);
                    }
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        chef.start();

        for(int i=0;i<4;i++){
            Thread customer=new Thread(()->{
                for(int j=0;j<100;j++){
                    if(Math.random()>0.5){
                        shop.sellBaozi(Xian.MEAT);
                    }else{
                        shop.sellBaozi(Xian.VEGG);
                    }
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            },"顾客"+i);
            customer.start();
        }
    }
}